//author: lkluever 

package com.me.mygdxgame;
import java.util.*;

public class  GameObject {
	
	protected ArrayList<Component> components = new ArrayList<Component>();
	
	//Add a Component to the ArrayList components of this GameObject
	public void addComponent(Component component){
		component.setParent(this);
		components.add(component);
	}
	//Remove a Component from the ArrayList components of this GameObject
	public void removeComponent(String component){
		components.remove(getIndexOfComponent(component));
	}
	
	//Get a Component by its name as String from the ArrayList components of this GameObject
	public Component getComponent(String component){
		return components.get(getIndexOfComponent(component));
	}
	
	//Get the index of a Component in the ArrayList components of this GameObject by its name as String
	int getIndexOfComponent(String component) {
		for(int i = 0; i < components.size(); i++){
			Class<? extends Component> currentComponentClass = components.get(i).getClass();
			if(currentComponentClass.getName().endsWith(component)){
				return i;
			}
		}
		return 0;
	}
}
